package com.pencilisland.pay.core.mp.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pencilisland.pay.api.bean.constant.WxpayConstant;
import com.pencilisland.pay.api.bean.mp.core.MessageRequest;
import com.pencilisland.pay.api.bean.mp.core.MessageResponse;
import com.pencilisland.pay.api.bean.mp.core.WxpayNotifyREQ;
import com.pencilisland.pay.api.bean.mp.core.WxpayNotifyRES;
import com.pencilisland.pay.api.bean.mp.mgt.SyncOrderREQ;
import com.pencilisland.pay.api.bean.mp.mgt.SyncOrderRES;
import com.pencilisland.pay.api.entity.GoodsEntity;
import com.pencilisland.pay.api.entity.TradeOrderEntity;
import com.pencilisland.pay.api.repository.GoodsRepository;
import com.pencilisland.pay.api.repository.OrderAddressRepository;
import com.pencilisland.pay.api.repository.TradeOrderRepository;
import com.pencilisland.pay.api.service.CacheService;
import com.pencilisland.pay.core.mp.service.MBService;
import com.pencilisland.pay.core.service.RestService;

/**
 * 报文逻辑处理.微信支付通知
 *
 * @author breeze
 */
@Service("MB_wxpayNotify")
public class WxpayNotifyMB implements MBService {

	Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	ObjectMapper objectMapper;
	@Autowired
	TradeOrderRepository tradeOrderRepo;
	@Autowired
	GoodsRepository goodsRepo;
	@Autowired
	CacheService cacheService;
	@Autowired
	OrderAddressRepository orderAddressRepo;
	@Autowired
	RestService restService;

	@Override
	public synchronized MessageResponse process(MessageRequest request) {
		WxpayNotifyREQ requestBean = getRequestBody(request);

		log.info("接收微信支付通知【{}】【{}】【{}】", requestBean.getOrderNo(), requestBean.getResult(),
				requestBean.getRenewStock());
		
		TradeOrderEntity tradeOrder = tradeOrderRepo.findOne(requestBean.getOrderNo());
		
		SyncOrderREQ syncOrder = new SyncOrderREQ();
		syncOrder.setTransactionId(tradeOrder.getTransactionId());
		syncOrder.setStatus(tradeOrder.getStatus());
		syncOrder.setFinishTime(tradeOrder.getFinishTime());
		syncOrder.setOrderNo(tradeOrder.getOrderNo());

		if (WxpayConstant.FAIL.equals(requestBean.getResult())) {
			
			syncOrder.setType("wxpay_fail");
			if ("1".equals(requestBean.getRenewStock())) {
				if (tradeOrder != null) {
					GoodsEntity goods = goodsRepo.findOne(tradeOrder.getGoodsId());
					if (goods != null) {
						goods.setStock(goods.getStock() + tradeOrder.getTradeNum());
						goodsRepo.save(goods);
						cacheService.clearGoods(goods.getCode());
					}
				}
			}
		}
		
		if(WxpayConstant.SUCCESS.equals(requestBean.getResult())){
			syncOrder.setType("wxpay_success");
		}
		
		SyncOrderRES syncOrderRes = restService.sendToMgtMP("syncOrder", syncOrder, SyncOrderRES.class);
		if(syncOrderRes.getSuccess()){
			tradeOrder.setSyncMgt("2");
			tradeOrderRepo.save(tradeOrder);
		}
		
		
		WxpayNotifyRES res = new WxpayNotifyRES(true, null, "OK");
		return res;
	}

	public WxpayNotifyREQ getRequestBody(MessageRequest request) {
		return objectMapper.convertValue(request.getBody(), WxpayNotifyREQ.class);
	}

}
